package com.it.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.reggie.commen.R;
import com.it.reggie.entity.*;
import com.it.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@SuppressWarnings("all")
@RestController
@RequestMapping("/statics")
public class EchartsController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrdersService ordersService;

    //菜系营收占比分析
    @GetMapping("/dishCash")
    public R method1() {


        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("sum(amount) as value", "name");
        //分组的时候进行一个非空判断
        queryWrapper.isNotNull("dish_id");
        queryWrapper.groupBy("dish_id");


        List<Map<String, Object>> dishCash = orderDetailService.listMaps(queryWrapper);

        return R.success(dishCash);
    }


    //套餐营收占比分析
    @GetMapping("/setmealCash")
    public R method2() {

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("sum(amount) as value", "name");
        queryWrapper.isNotNull("setmeal_id");
        queryWrapper.groupBy("setmeal_id");

        List<Map<String, Object>> setmealCash = orderDetailService.listMaps(queryWrapper);

        return R.success(setmealCash);

    }


    //菜系销量占比分析
    @GetMapping("/dishNum")
    public R method3() {


        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        //根据菜品分类，计算相同菜品价格，查询菜品价格和菜品名称
        queryWrapper.select("sum(amount) as value", "name");
        queryWrapper.isNotNull("dish_id");
        queryWrapper.groupBy("dish_id");

        //存储查询结果
        List<Map<String, Object>> listMaps = orderDetailService.listMaps(queryWrapper);

        for (Map<String, Object> listMap : listMaps) {

            //遍历获取每一个菜品名称
            String name = (String) listMap.get("name");

            LambdaQueryWrapper<Dish> dishqw = new LambdaQueryWrapper<>();

            dishqw.eq(Dish::getName, name);
            //根据菜品名称查询菜品数据
            Dish one = dishService.getOne(dishqw);
            if (one != null) {
                //获取菜系id
                Long categoryId = one.getCategoryId();

                //根据id获取菜系名称
                Category categoryById = categoryService.getById(categoryId);

                //给每一个菜品的名称都换成菜系名称
                listMap.put("name", categoryById.getName());


            }

        }


        for (int i = 0; i < listMaps.size(); i++) {

            for (int y = 0; y < listMaps.size(); y++) {
                //遍历判断结果
                if (listMaps.get(i).get("name").equals(listMaps.get(y).get("name")) && i != y) {

                    BigDecimal value = (BigDecimal) listMaps.get(i).get("value");
                    BigDecimal value1 = (BigDecimal) listMaps.get(y).get("value");
                    BigDecimal valueSum = value.add(value1);

                    listMaps.get(y).put("value", valueSum);

                    listMaps.remove(listMaps.get(i));

                    break;

                }
            }
            System.out.println(listMaps);
        }
        return R.success(listMaps);
    }

    //套餐销量占比分析
    @GetMapping("/setmealNum")
    public R method4() {


        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        //根据菜品分类，计算相同菜品价格，查询菜品价格和菜品名称
        queryWrapper.select("sum(amount) as value", "name");
        queryWrapper.isNotNull("setmeal_id");
        queryWrapper.groupBy("setmeal_id");

        //存储查询结果
        List<Map<String, Object>> listMaps = orderDetailService.listMaps(queryWrapper);

        for (Map<String, Object> listMap : listMaps) {

            //遍历获取每一个菜品名称
            String name = (String) listMap.get("name");

            LambdaQueryWrapper<Setmeal> setmealqw = new LambdaQueryWrapper<>();

            setmealqw.eq(Setmeal::getName, name);
            //根据菜品名称查询菜品数据
            Setmeal one = setmealService.getOne(setmealqw);
            if (one != null) {
                //获取菜系id
                Long categoryId = one.getCategoryId();

                //根据id获取菜系名称
                Category categoryById = categoryService.getById(categoryId);


                //给每一个菜品的名称都换成菜系名称
                listMap.put("name", categoryById.getName());


            }

        }


        for (int i = 0; i < listMaps.size(); i++) {

            for (int y = 0; y < listMaps.size(); y++) {
                //遍历判断结果
                if (listMaps.get(i).get("name").equals(listMaps.get(y).get("name")) && i != y) {

                    BigDecimal value = (BigDecimal) listMaps.get(i).get("value");
                    BigDecimal value1 = (BigDecimal) listMaps.get(y).get("value");
                    BigDecimal valueSum = value.add(value1);

                    listMaps.get(y).put("value", valueSum);

                    listMaps.remove(listMaps.get(i));

                    break;

                }
            }
            System.out.println(listMaps);
        }
        return R.success(listMaps);

    }

    //月营业额趋势图
    @GetMapping("/cashPerMonth")
    public Map method5() {

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();

        queryWrapper.select(" sum(amount)", "checkout_time");
        queryWrapper.groupBy("checkout_time");

        List<Map<String, Object>> listMaps = ordersService.listMaps(queryWrapper);

        List list1 = new ArrayList();
        List list2 = new ArrayList();

        for (Map<String, Object> listMap : listMaps) {

            Set<Map.Entry<String, Object>> entries = listMap.entrySet();

            for (Map.Entry<String, Object> entry : entries) {

                if (entry.getKey().equals("checkout_time")) {

                    list1.add(entry.getValue().toString());
                } else {
                    list2.add(entry.getValue());
                }

            }

        }


        Map map = new HashMap();

        map.put("monthList", list1);
        map.put("dataList", list2);


        return map;

    }

}
